package com.leyou.auth.properties;

import com.leyou.auth.utils.RsaUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;

@Slf4j
@ConfigurationProperties(prefix = "leyou.jwt")
public class JwtProperties {

    private String secret; // 密钥

    private String pubKeyPath;// 公钥

    private String priKeyPath;// 私钥

    private Integer expire;// token过期时间

    private PublicKey publicKey; // 公钥

    private PrivateKey privateKey; // 私钥
    private Integer cookieMaxLiveTimeMinute;


    /**
     * @PostContruct：在构造方法执行之后执行该方法
     */
    @PostConstruct
    public void init(){
        try {
            File pubKey = new File(pubKeyPath);
            File priKey = new File(priKeyPath);
            if (!pubKey.exists() || !priKey.exists()) {
                String path = priKeyPath.substring(0, priKeyPath.lastIndexOf("/"));
                File pathFile = new File(path);
                pathFile.mkdirs();
                // 生成公钥和私钥
                RsaUtils.generateKey(pubKeyPath, priKeyPath, secret);
            }
            // 获取公钥和私钥
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
            this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
        } catch (Exception e) {
            log.error("初始化公钥和私钥失败！", e);
            throw new RuntimeException();
        }
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        if (StringUtils.isBlank(secret)) {
            secret = JwtConstans.SECRET ;
        }
        this.secret = secret;
    }

    public String getPubKeyPath() {
        return pubKeyPath;
    }

    public void setPubKeyPath(String pubKeyPath) {
        if (StringUtils.isBlank(pubKeyPath)) {
            pubKeyPath = JwtConstans.PUBKEY_PATH ;
        }
        this.pubKeyPath = pubKeyPath;
    }

    public String getPriKeyPath() {
        return priKeyPath;
    }

    public void setPriKeyPath(String priKeyPath) {
        if (StringUtils.isBlank(priKeyPath)) {
            priKeyPath = JwtConstans.PRIKEY_PATH;
        }
        this.priKeyPath = priKeyPath;
    }

    public int getExpire() {
        return expire;
    }

    public void setExpire(Integer expire) {
        if (expire == null) {
            expire = JwtConstans.EXPIRE ;
        }
        this.expire = expire;
    }

    public PublicKey getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(PublicKey publicKey) {
        this.publicKey = publicKey;
    }

    public PrivateKey getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(PrivateKey privateKey) {
        this.privateKey = privateKey;
    }

    public Integer getCookieMaxLiveTimeMinute() {
        return cookieMaxLiveTimeMinute;
    }

    public void setCookieMaxLiveTimeMinute(Integer cookieMaxLiveTimeMinute) {
        if (cookieMaxLiveTimeMinute == null) {
            cookieMaxLiveTimeMinute = JwtConstans.COOKIE_MAX_LIVE_TIME_MINUTE;
        }
        this.cookieMaxLiveTimeMinute = cookieMaxLiveTimeMinute;
    }
    // getter setter ...
}